package com.pointlion.sys.mvc.common.model;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.pointlion.sys.mvc.common.dto.ZtreeNode;
import com.pointlion.sys.mvc.common.model.base.BasePaperBase;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("serial")
public class PaperBase extends BasePaperBase<PaperBase> {
	public static final PaperBase dao = new PaperBase();
	public static final String tableName = "paper_base";
	
	/***
	 * ����������ѯ
	 */
	public PaperBase getById(String id){
		return PaperBase.dao.findById(id);
	}
	
	/***
	 * ɾ��
	 * @param ids
	 */
	@Before(Tx.class)
	public void deleteByIds(String ids){
    	String idarr[] = ids.split(",");
    	for(String id : idarr){
    		PaperBase o = PaperBase.dao.getById(id);
    		o.delete();
    	}
	}

    public List<PaperBase> getAllPageBase() {
		List<PaperBase> list =  getChildrenAll("#root");
		return list;
    }

	/***
	 * 递归
	 * 查询孩子
	 * @param id
	 * @return
	 */
	public List<PaperBase> getChildrenAll(String id){
		List<PaperBase> menuList =  getChildrenByPid(id);//根据id查询孩子
		for(PaperBase m : menuList){
			m.setChildren(getChildrenAll(m.getId()));
		}
		return menuList;
	}

	/***
	 * 根据id 查询孩子
	 * @param id
	 * @return
	 */
	public List<PaperBase> getChildrenByPid(String id){
		return PaperBase.dao.find("select * from paper_base m where m.parent_id='"+id+"' order by m.sort");
	}

	public List<PaperBase> getAllMenu() {
		List<PaperBase> list =  getChildrenAll("#root");
		return list;
	}

	/***
	 * 菜单转成ZtreeNode
	 * @param p
	 * @return
	 */
	public ZtreeNode toZTreeNode(PaperBase p){
		ZtreeNode node = new ZtreeNode();
		node.setId(p.getId());
		node.setName(p.getName());
		return node;
	}
	/***
	 * 菜单转成ZTreeNode
	 * @param menuList
	 * @return
	 */
	public List<ZtreeNode> toZTreeNode(List<PaperBase> menuList,Boolean open){
		List<ZtreeNode> list = new ArrayList<ZtreeNode>();
		for(PaperBase p : menuList){
			ZtreeNode node = toZTreeNode(p);
			if(p.getChildren()!=null&&p.getChildren().size()>0){
				node.setChildren(toZTreeNode(p.getChildren(),open));
				node.setIcon(p.getCode());
			}
			node.setOpen(open);
			//icon用来存放指标权重
			node.setIcon(p.getCode());
			list.add(node);
		}
		return list;
	}
}